package com.hyz.reggie.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyz.reggie.dao.SetmealDao;
import com.hyz.reggie.dto.SetmealDto;
import com.hyz.reggie.pojo.Category;
import com.hyz.reggie.pojo.Dish;
import com.hyz.reggie.pojo.Setmeal;
import com.hyz.reggie.pojo.SetmealDish;
import com.hyz.reggie.service.CategoryService;
import com.hyz.reggie.service.DishService;
import com.hyz.reggie.service.SetmealDishService;
import com.hyz.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private CategoryService service;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public IPage<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qwq = new LambdaQueryWrapper<>();
        if (name != null) {
            qwq.like(Setmeal::getName, name);
        }
        iPage = page(iPage, qwq);
        List<Setmeal> setmeals = iPage.getRecords();
        List<SetmealDto> setmealDtos = JSON.parseArray(JSON.toJSONString(setmeals), SetmealDto.class);
        setmealDtos.stream().forEach(s -> {
            Long categoryId = s.getCategoryId();
            Category category = service.getById(categoryId);
            s.setCategoryName(category.getName());
        });
        IPage<SetmealDto> newIpage = new Page<>();
        BeanUtils.copyProperties(iPage, newIpage);
        newIpage.setRecords(setmealDtos);
        return newIpage;
    }

    @Override
    public void add(SetmealDto setmealDto) {
        save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach(s -> {
            s.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> qwq = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SetmealDish> eq = qwq.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(qwq);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIds(Long[] ids) {
        removeByIds(Arrays.asList(ids));
        Arrays.stream(ids).forEach(c -> {
            LambdaQueryWrapper<SetmealDish> qwq = new LambdaQueryWrapper<>();
            qwq.eq(SetmealDish::getSetmealId, c);
            List<SetmealDish> setmealDishList = setmealDishService.list(qwq);
            setmealDishList.stream().forEach(s -> {
                setmealDishService.removeById(s.getId());
            });

        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifySetmeal(SetmealDto setmealDto) {
        updateById(setmealDto);
        Long semealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> qwq = new LambdaQueryWrapper<>();
        qwq.eq(SetmealDish::getSetmealId, semealId);
        setmealDishService.remove(qwq);
        setmealDishes.stream().map(s -> {
            s.setSetmealId(semealId);
            return s;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void startOrEnd(Integer status, Long[] ids) {
        List<Setmeal> setmeals = listByIds(Arrays.asList(ids));
        setmeals.stream().map(s -> {
            s.setStatus(status);
            return s;
        }).collect(Collectors.toList());
        updateBatchById(setmeals);
    }

    @Override
    public List<Setmeal> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> qwq = new LambdaQueryWrapper<>();
        qwq.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        qwq.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        qwq.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = list(qwq);
        return list;
    }
}
